import 'bootstrap/dist/js/bootstrap.js';
require('./swiper.min.js');
require('./WebSocketServiceClass.js');
import j from  'jquery';
window.jQuery = j;
import {webSocketServiceHost,useWebSocket,areaShielding,createewm ,loginStatus,templatetype,log} from './urlconfig/config.js';
var Fingerprint = require('./fingerprint.js');
var interval;
var token;
var cpuid = new Fingerprint().get();
var edition='web_1.5.1.2';
var channel;
var sys='Window10';
function GetUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;

        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");

            if (arr != null && arr[0] == paraName) {
                return arr[1];
            }
        }
        return "";
    }
    else {
        return "";
    }
};
(function (){
    var url = document.location.toString();
    if (url.indexOf("?") != -1) {
        channel=GetUrlParam("c_code");
        localStorage.setItem("channel",channel);
    }else{
        if(localStorage.channel !=null){
            channel=localStorage.getItem("channel");
        }else{
            channel='plat_a01';
        }
    }
})();
//(function (){
//    $.ajax({
//        type:'post',
//        url:areaShielding,
//        dataType:'json',
//        success:function (res){
//            console.log(res.data.screen);
//            if(res.data.screen==1){
//                $("#model-show-1").show();
//                $("#office-show-1").show();
//                $("#model-show-3").remove();
//                $("#office-show-3").remove();
//            }else{
//                $("#model-show-3").show();
//                $("#office-show-3").show();
//                $("#model-show-1").remove();
//                $("#office-show-1").remove();
//            }
//        },
//        error:function (err){
//            console.log(err)
//        }
//    })
//})();
(function (){
    if(localStorage.token != null){
        var token=localStorage.token;
            var src=localStorage.src;
            var name=localStorage.name;
            var $dropdown=$('<div class="login-up" >'+
                ' <span class="name">'+name+
                '</span>'+
                '<img src="'+ src + '"'+'class="yonghu">'+
                '</div>');
            $(".dropdown").empty();
            $(".dropdown").append($dropdown);
    }
})();
var main;
if($("#fagepage").length>0){
    main=$("#fagepage").val();
}
/*移入显示下载*/
$(function (){
    $(document).on('mouseover','.content .items li',function(){
        $(this).css({'z-index': '8000'});
        $(".mask, .download", $(this)).show();
    });
    $(document).on('mouseout','.content .items li',function(){
        $(this).css({'z-index': '0'});
        $(".mask, .download", $(this)).hide();
    });
});
/*轮播*/
var s = new Swiper(".swiper-container", {
    loop: true,//->运动到最后一张可以实现无缝衔接
    autoplay:3000,//->每隔3000ms自动轮播*/
    autoplayDisableOnInteraction: true,
    pagination: ".swiper-pagination",//->设置分页器*/
    speed: 500,//->切换的速度,默认是300ms
    paginationClickable :true
});
/*回到顶部*/
$(function (){
    $(document).scroll(function(){
        var  scrollTop =  $(document).scrollTop(),bodyHeight = $(window).height();
        if(scrollTop > bodyHeight){
            $('#go_top').css('display','block');
        }else{
            $('#go_top').css('display','none');
        }
    });
    $("#go_top").click(function (){
        $('html , body').animate({scrollTop: 0},'slow');
    })
});

var scene_id,WebSocketServiceObject;
/*请求二维码*/
function login(){
    $.ajax({
        type:'post',
        url:createewm,
        dataType:'json',
        success:function (res){
            if(res.code === 200){
                $(".bc-img").empty();
                var $login= $(
                    '<img src="'+ res.data.url + '">'
                  )
            }
            $(".bc-img").append($login);
            scene_id =res.data.scene_id;
            if(scene_id !=null){
                if(useWebSocket == 1){
                    useWebSocketService();
                }else{
                    status()
                }
            }
        },
        error:function (err){

        }
    })
}
/*登录*/
$('#login').on('show.bs.modal', function (e) {
    login();
    tab('userLoginMod/loginWindow')
});


/*请求登录状态*/
$('#login').on('hide.bs.modal', function (e) {
    if(useWebSocket == 1){
        WebSocketServiceObject.close();
        WebSocketServiceObject = null;
    }else{
        clearInterval(interval);
    }

});
$("#login").click(function (){
    window.localStorage.removeItem('data');
});
function useWebSocketService(){
    if(WebSocketServiceObject == null){
        WebSocketServiceObject = new  WebSocketServiceClass(webSocketServiceHost)
        WebSocketServiceObject.addCallBack("login",loginStatusCheck)

        const reConnection = function (data,the,name) {
            //console.log("reConnectionByError websocket发生错误，检测是否重连" + the.status)
            if(  (the.status == -1 && name == 'error')  || (the.status == 3 && name == 'close') ){
                //console.log("reConnectionByError websocket发生错误，1秒后重连" + the.status)
                setTimeout(function () {
                    reConnectionWebSocketService()
                },1000)
            }
        }

        WebSocketServiceObject.addCallBack("error",reConnection)
        WebSocketServiceObject.addCallBack("close",reConnection)
    }
    WebSocketServiceObject.check(scene_id)
}

function reConnectionWebSocketService() {
    WebSocketServiceObject = null;
    console.log("websocket发生错误，重连");
    useWebSocketService();
}

function loginStatusCheck(ret){
    console.log(ret);

    if (ret.code != 200){
        return false;
    }

    let res = ret.data;
    $('#login').modal('hide');
    tab('userRegister');
    token=res.data.token;
    var name=res.data.nickname;
    var src= res.data.headimgurl;
    var $dropdown='<div class="login-up">'+
        ' <span class="name">'+name+
        '</span>'+
        '<img src="'+src + '"'+'class="yonghu">'+
        '</div>';
    $(".dropdown").empty();
    $(".login-content .p2").remove();
    $(".dropdown").append($dropdown);
    localStorage.setItem("token",token);
    localStorage.setItem("name",name);
    localStorage.setItem("src",src);
    window.location.reload();
}


function status(){
    interval = setInterval(function () {
        $.ajax({
            type:'post',
            url:loginStatus,
            dataType:'json',
            data:{
                scene_id:scene_id
            },
            success:function (res){
                if(res.code === 200){
                    $('#login').modal('hide');
                    tab('userLoginMod/userRegister',false);
                    token=res.data.token;
                    var name=res.data.nickname;
                    var src= res.data.headimgurl;
                    var $dropdown='<div class="login-up">'+
                        ' <span class="name">'+name+
                        '</span>'+
                        '<img src="'+src + '"'+'class="yonghu">'+
                        '</div>';
                    $(".dropdown").empty();
                    $(".login-content .p2").remove();
                    $(".dropdown").append($dropdown);
                    localStorage.setItem("token",token);
                    localStorage.setItem("name",name);
                    localStorage.setItem("src",src);
                    window.location.reload();
                    clearInterval(interval);
                }

            },
            error:function (err){
                    console.log(err)
            }
        })
    },2000)

}

/*退出登录*/


$(".dropdown-menus").click(function (){
    $(this).hide();
    loginout();
    tab('closeJump',false);
    window.localStorage.removeItem('data');
    localStorage.removeItem("token");
    localStorage.removeItem("name");
    localStorage.removeItem("src");
});
if($(".login-up").length>0){
    $('.navbar-nav').on('click','.dropdown',function(){
        if($(".dropdown-menus").is(':hidden')){
            $(".dropdown-menus").show();
        }else{
            $(".dropdown-menus").hide()
        }
    });
}


function loginout(){
    var $loginButton = $('<span  class="dropdown-toggle profile denglu"   data-toggle="modal" data-target="#login" aria-expanded="true">登录</span>');
    $(".dropdown").empty().append($loginButton);
    $(".dropdown-menus").remove();
    //window.location.reload();

}
$(".denglu").click(function (){
    window.localStorage.removeItem('data');
});

$("#nav-all a").click(function(){
    var btn=$(this).attr("data-log");
    tab(btn,true)
});

//logo点击
$(".topbar-left").click(function(){
    var btn=$(this).attr("data-log");
    tab(btn,true)
});
/*跳出*/
$(".dropdown-menus a").click(function(){
    btnclick('userLoginOut')
});

$(".swiper-slide a").click(function(){
    var btn=$(this).attr("data-log");
    btnclick(btn)
});
$(".dropdown-menus").click(function(){
    tab('userLoginMod/closeJump',true)
});
/*关闭*/
$(".login-close").click(function (){
    window.localStorage.removeItem('data');
    var btn=$(this).attr("data-log");
    tab(btn,true)
});
function btnclick(name){
    $.ajax({
        type:'GET',
        url:log+main+'/'+ name +'/'+ channel+'/'+ edition + '/'+ sys + '/'+cpuid,
        dataType: 'json',
        success:function(res) {
            if(res.code==200){

            }
        },
        error:function(err) {


        }
    })
}
$(".kefuclick").click(function (){
    var tablog=$(this).attr("data-log");
    tab(tablog,true);

});
function tab(name,boolen){
    $.ajax({
        type:'GET',
        url:log+ name+'/'+ channel+'/'+ edition + '/'+sys+'/'+cpuid,
        dataType: 'json',
        async:boolen,
        success:function(res) {
            if(res.code==200){

            }
        },
        error:function(err) {

        }
    });
}

